define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/dom',
      'dojo/json',
      'dojo/on',
      'dojo/dom-construct',
      'dojo/Evented',
      'dijit/_WidgetBase'
   ],
   function(declare, lang, dom, JSON, on, domConstruct, Evented, WidgetBase)
   {
      return declare([WidgetBase, Evented],
      {
         id:"OslcWidgetDelegatedResourceBaseWidget",
         mSignal: null,
         constructor:function(args) 
         {
            // Call Super
            dojo.safeMixin(this, args);
         },
         postscript:function(args) 
         {
            this.inherited(arguments);

            this.mSignal = on(window, 'message', lang.hitch(this, function(e)
            {
               var HEADER = "oslc-response:";
               if (e.source != dom.byId("OslcWidgetDelegatedResourceBaseWidgetIframe").contentWindow || e.data.indexOf(HEADER) !== 0)
                  return;

               this.mSignal.remove();
               var data = (e.data.substr(HEADER.length));
               if(data === '')
               {
                  this.gotResult(null);
                  return;
               }
               var jsonData = JSON.parse(data);

               var results = jsonData['oslc:results'];
               this.preResultsEvent(results);
               if (!results || results.length === 0)
               {
                  this.gotResult(null);
                  return;
               }

               for(var i = 0; i < results.length; ++i)
                  this.gotResult(results[i]);

               this.postResultsEvent(results);
            }));

            dojo.connect(this, "onHide", this, function(e) 
            {
               dojo.hitch(this, setTimeout(function() 
               {
                  this.mSignal.remove();
                  registry.byId("OslcDialogDelegatedResourceBaseDlg").destroyRecursive();
               }, 50));
            });
         },
         displayUrl: function()
         {
            domConstruct.create("iframe", 
            {
               src: this.dialogUrl + '#oslc-core-postMessage-1.0',
               id:"OslcWidgetDelegatedResourceBaseWidgetIframe",
               style: {height:this.height, width: this.width, scrolling:'auto', border:0}
            }, this.domNode);
         },
         uninitialize: function()
         {
            if(this.mSignal)
               this.mSignal.remove();

            this.mSignal = null;
            this.inherited(arguments);
         },
         gotResult:function(result, results) {
         },
         preResultsEvent:function(results) {
         },
         postResultsEvent:function(results) {
         }
      });
   }
);
